<?
class iMVC_DB_Mysql_Reader extends iMVC_DB_Reader_Abstract{
    private $_mysqli;

 /* :参数非空验证:2011-11-04 14:21:53:IT研发:  */


    function __construct($mysqli){

        $this->_mysqli=$mysqli;



    }
    public function read(){

        $stmt=$this->_mysqli->prepare('SELECT * From table1');
        $stmt->execute();
        $stmt->bind_result($res);
        while($stmt->fetch()){
            var_dump($res);
        }
        $stmt->close();
        return $res;

    } 
    public function readByPk($pk,$pk_value,$table){

        $sql=sprintf('SELECT * FROM %s where %s=%s',$table,$pk,$pk_value); 
        $rs=$this->_mysqli->query($sql);
        $rsArray=$rs->fetch_array(MYSQLI_ASSOC);
        return $rsArray;
    }

    public function readAll($table,$orderItem=null,$order='asc'){
        if($orderItem!=null){
            $sql=sprintf('SELECT * FROM %s ORDER BY %s %s ',$table,$orderItem,$order);
        }
        else{
            $sql=sprintf('SELECT * FROM %s',$table);
        }
        $rs=$this->_mysqli->query($sql);
        if($rs==null){
            throw new Exception('iMVC_Db_Mysql_Reader ERROR No data found,check your sql'); 
        }
        while($item=$rs->fetch_array(MYSQLI_ASSOC)){
            $rsArray[]=$item; 
        }
        return $rsArray;
    }
    public function readByLimit($table,$start=0,$count,$orderItem=null,$order='asc'){
        if($orderItem!=null){
            $sql=sprintf('SELECT * FROM %s ORDER BY %s %s LIMIT %s,%s',$table,$orderItem,$order,$start,$count);
        }
        else{

            $sql=sprintf('SELECT * FROM %s LIMIT %s,%s',$table,$start,$count);
        }
        $rs=$this->_mysqli->query($sql);
    
        while($item=$rs->fetch_array(MYSQLI_ASSOC)){
            $rsArray[]=$item; 
        }
        return $rsArray;
    }

    public function readByQuery($sql){
        $rs=$this->_mysqli->query($sql);
         while($item=$rs->fetch_array(MYSQLI_ASSOC)){
            $rsArray[]=$item; 
        }
        return $rsArray;
    
    }

}


// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s
//
